package managedbean;

import entity.Employee;
import entity.RedirectProperties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpSession;
import session.stateless.EmployeeBeanLocal;

@ManagedBean(name="controller") 
@SessionScoped
public class Controller {
    private String message; 
    private String userName; 
    private String password;
    private String userType;
    private Employee employee;
    private String statusMessage;
    
    @EJB
    private EmployeeBeanLocal employeeSessionBeanLocal;
    
    public String getMessage() { 
        return message; 
    } 


    public void setMessage(String message) { 
        this.message = message; 
    } 


    public String getUserName () { 
        return userName; 
    } 


    public void setUserName (String username) { 
        this.userName = username; 
    } 


    public String getPassword () { 
        return password; 
    } 


    public void setPassword (String password) { 
        this.password = password; 
    }

    public String getUserType() {
        return userType;
    }

    public void setUserType(String userType) {
        this.userType = userType;
    }

    public Employee getEmployee() {
        return employee;
    }

    public void setEmployee(Employee employee) {
        this.employee = employee;
    }

    public void login(){
        
        String temp_UserName = String.valueOf(userName);
        String temp_Password = String.valueOf(password);
        String temp_UserType = String.valueOf(userType);
        
        if (temp_UserType.equals("staff")){
            try { 
                
                employee = employeeSessionBeanLocal.find(userName, password);
                if (employee != null){
                    //this.userName = this.password = null;
                    ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
                    RedirectProperties rp = new RedirectProperties();
                    ec.redirect(rp.getUrl() + "/innersystem/index.xhtml");
                } 
                else{
                    //setMessage("Unknown login, please retry.");
                    statusMessage = "\n\n\nWrong username or password.\nPlease try again!"; 
                    FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, this.statusMessage, "")); 
                }
                
            } 
            catch(Exception ex) 
            { 
                ex.printStackTrace(); 
            } 
        }
        
//        if (temp_UserType.equals("customer")){
//            try { 
//                CustomerManager customerManager = new CustomerManager(customerSessionBeanLocal);
//
//                customerId = customerManager.verifyLogin(userName, password);
//                if (customerId == null){
//                }
//                else{
//                    FacesContext.getCurrentInstance().getExternalContext().redirect("innersystem/indexCustomer.xhtml");
//                } 
//            } 
//            catch(Exception ex) 
//            { 
//                ex.printStackTrace(); 
//            } 
//        }
    }
    
    public void logOut() throws ServletException {
        this.employee = null;
        
        ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
            try {
                //kill the session
                ((HttpSession)ec.getSession(true)).invalidate();
                RedirectProperties rp = new RedirectProperties();
                ec.redirect(rp.getUrl() + "/index.xhtml");
            } catch (Exception ex) {
                Logger.getLogger(Controller.class.getName()).log(Level.SEVERE, null, ex);
            }
        

//        try { 
//            //FacesContext.getCurrentInstance().getExternalContext().getSessionMap().remove("username");
//            FacesContext.getCurrentInstance().getExternalContext().redirect("index.xhtml");
//        }
//                    catch(Exception ex) 
//            { 
//                ex.printStackTrace(); 
//            } 
    }
    
    public boolean isLoggedIn(){
        return (employee != null);
    }
    
    public boolean isNotLoggedIn(){
        return (employee == null);
    }
    
    public boolean isAdmin(){
        if (employeeSessionBeanLocal.isAdmin(this.getEmployee().getEmployeeId()))
            System.out.println("@@@@");
        return employeeSessionBeanLocal.isAdmin(this.getEmployee().getEmployeeId());
    }
    
    public boolean isCRMStaff(){
        return employeeSessionBeanLocal.isCRMStaff(this.getEmployee().getEmployeeId());
    }
    
    public boolean isFRMStaff(){
        return employeeSessionBeanLocal.isFRMStaff(this.getEmployee().getEmployeeId());
    }    
    
    public boolean isMRPStaff(){
        return employeeSessionBeanLocal.isMRPStaff(this.getEmployee().getEmployeeId());
    }
    
    public boolean isSCMStaff(){
        return employeeSessionBeanLocal.isSCMStaff(this.getEmployee().getEmployeeId());
    }
}
